package com.schoolvisitors.controller.admin;

import com.alibaba.fastjson2.JSONObject;
import com.schoolvisitors.CheckPermission;
import com.schoolvisitors.dto.UpdateUserDtoByManage;
import com.schoolvisitors.dto.UserInfoDto;
import com.schoolvisitors.dto.UserRegisterDto;
import com.schoolvisitors.entity.User;
import com.schoolvisitors.enums.PermissionEnum;
import com.schoolvisitors.response.ResultBody;
import com.schoolvisitors.service.UserService;
import com.schoolvisitors.vo.UserByManageVo;
import jakarta.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/v1/admin")
public class ManageUserController {
    private final UserService userService;

    public ManageUserController(UserService userService) {
        this.userService = userService;
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("/createUser")
    public ResultBody register(@RequestBody @Valid UserRegisterDto userRegisterDto) {
        userService.register(userRegisterDto);
        return ResultBody.success();
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("/updateUser")
    public ResultBody updateUser(@RequestBody @Valid UpdateUserDtoByManage dto) {
        userService.updateUser(dto);
        return ResultBody.success();
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("/deleteUser")
    public ResultBody deleteUser(@RequestBody JSONObject json) {
        Long id = json.getLong("id");
        userService.deleteUser(id);
        return ResultBody.success();
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @GetMapping("/getUserInfo")
    public ResultBody getUserInfo(@Param(value = "userId") Long userId) {
        UserInfoDto userInfoDto = userService.getUserInfo(userId);
        return ResultBody.success(userInfoDto);
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @GetMapping("/getNotReviewUsers")
    public ResultBody getNotReviewUsers(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size
    ){
        Page<User> userPage = userService.findUsersByNotReview(page, size);
        List<UserByManageVo> userVos = new ArrayList<>();
        for (User user : userPage.getContent()) {
            UserByManageVo userVo = UserByManageVo.makeUserByManageVo(user);
            userVos.add(userVo);
        }
        JSONObject data = new JSONObject();
        data.put("users", userVos);
        data.put("total", userPage.getTotalElements());
        return ResultBody.success(data);


    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @GetMapping("/getUsers")
    public ResultBody getUsers(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String search
    ) {
        Page<User> userPage = userService.findUsers(page, size, search);
        List<UserByManageVo> userVos = new ArrayList<>();
        for (User user : userPage.getContent()) {
            UserByManageVo userVo = UserByManageVo.makeUserByManageVo(user);
            userVos.add(userVo);
        }
        JSONObject data = new JSONObject();
        data.put("users", userVos);
        data.put("total", userPage.getTotalElements());
        return ResultBody.success(data);
        //return new PageResult<>(userPage.getContent(), userPage.getTotalElements());
    }



    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("/rejectReservationPermission")
    public ResultBody rejectReservationPermission(@RequestBody JSONObject json) {
        Long userId = json.getLong("userId");
        userService.reviewReservationPermission(userId, false);
        return ResultBody.success();
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("/approveReservationPermission")
    public ResultBody approveReservationPermission(@RequestBody JSONObject json) {
        Long userId = json.getLong("userId");
        userService.reviewReservationPermission(userId, true);
        return ResultBody.success();
    }

///
}
